home *** CD-ROM | disk | FTP | other *** search
/ Programming a Multiplayer FPS in DirectX / Programming a Multiplayer FPS in DirectX (Companion CD).iso / DirectX / dxsdk_oct2004.exe / dxsdk.exe / Samples / Media / SharedFx / specular.fx < prev   
Encoding:
Text File  |  2004-09-27  |  6.1 KB  |  171 lines

  1. //-----------------------------------------------------------------------------
  2. // File: Flyer.fx
  3. //
  4. // Desc: The effect file for the EffectMesh sample.  The technique implements:
  5. //
  6. //       Texture mapping
  7. //       Diffuse lighting
  8. //       Specular lighting
  9. //       Environment mapping
  10. // 
  11. // Copyright (c) Microsoft Corporation. All rights reserved.
  12. //-----------------------------------------------------------------------------
  13.  
  14.  
  15. //-----------------------------------------------------------------------------
  16. // Global variables
  17. //-----------------------------------------------------------------------------
  18. texture g_txScene;  // texture for scene rendering
  19.  
  20. shared float4x4 g_mWorldView : WORLDVIEW;               // View matrix for object
  21. shared float4x4 g_mWorldViewProjection : WORLDVIEWPROJECTION; // World * View * Projection matrix
  22. //float4x4 g_mProj;                                       // Projection matrix for object
  23. shared float4 g_vLightColor = {1.0f, 1.0f, 1.0f, 1.0f}; // Light value
  24. shared float  g_fTime;                                  // Time value
  25. shared float3 g_vLight;                                 // Light position in view space
  26. float  g_fSizeMul = 1.0f;                               // A size multiplier
  27. float  g_fAnimSpeed;                                    // Animation speed
  28.  
  29. // Object material attributes
  30. float4 Diffuse;      // Diffuse color of the material
  31. float4 Specular = {1.0f, 1.0f, 1.0f, 1.0f};  // Specular color of the material
  32. float  Power = 1.0f;
  33.  
  34.  
  35. //-----------------------------------------------------------------------------
  36. // Texture samplers
  37. //-----------------------------------------------------------------------------
  38. sampler g_samScene =
  39. sampler_state
  40. {
  41.     Texture = <g_txScene>;
  42.     MinFilter = Linear;
  43.     MagFilter = Linear;
  44.     MipFilter = None;
  45. };
  46.  
  47.  
  48. //-----------------------------------------------------------------------------
  49. // Name: VertScene
  50. // Type: Vertex shader
  51. // Desc: This shader computes standard transform and lighting
  52. //-----------------------------------------------------------------------------
  53. void VertScene( float4 vPos : POSITION,
  54.                 float3 vNormal : NORMAL,
  55.                 float2 vTex0 : TEXCOORD0,
  56.                 out float4 oPos : POSITION,
  57.                 out float4 oDiffuse : COLOR0,
  58.                 out float2 oTex0 : TEXCOORD0,
  59.                 out float3 oViewPos : TEXCOORD1,
  60.                 out float3 oViewNormal : TEXCOORD2 )
  61. {
  62.     // Transform the position from object space to homogeneous projection space
  63.     oPos = mul( vPos, g_mWorldViewProjection );
  64. //    oPos = mul( vPos, g_mWorldView );
  65. //    oPos = mul( oPos, g_mProj );
  66.  
  67.     // Compute the view-space position
  68.     oViewPos = mul( vPos, g_mWorldView );
  69.  
  70.     // Compute view-space normal
  71.     oViewNormal = normalize( mul( vNormal, (float3x3)g_mWorldView ) );
  72.  
  73.     // Compute lighting
  74.     oDiffuse = dot( oViewNormal, normalize( g_vLight - oViewPos ) ) * Diffuse;
  75.  
  76.     // Just copy the texture coordinate through
  77.     oTex0 = vTex0;
  78. }
  79.  
  80.  
  81. //-----------------------------------------------------------------------------
  82. // Name: PixScene
  83. // Type: Pixel shader
  84. // Desc: This shader outputs the pixel's color by modulating the texture's
  85. //         color with diffuse material color
  86. //-----------------------------------------------------------------------------
  87. float4 PixScene( float4 MatDiffuse : COLOR0,
  88.                  float2 Tex0 : TEXCOORD0,
  89.                  float3 ViewPos : TEXCOORD1,
  90.                  float3 ViewNormal : TEXCOORD2 ) : COLOR0
  91. {
  92.     // Compute half vector for specular lighting
  93.     float3 vHalf = normalize( normalize( -ViewPos ) + normalize( g_vLight - ViewPos ) );
  94.  
  95.     // Compute normal dot half for specular light
  96.     float4 fSpecular = pow( saturate( dot( vHalf, normalize( ViewNormal ) ) ) * Specular, Power );
  97.  
  98.     return float4( (float3)( g_vLightColor * ( tex2D( g_samScene, Tex0 ) * MatDiffuse + fSpecular ) ), 1.0f );
  99. }
  100.  
  101.  
  102. void VertScene1x( float4 vPos : POSITION,
  103.                   float3 vNormal : NORMAL,
  104.                   float2 vTex0 : TEXCOORD0,
  105.                   out float4 oPos : POSITION,
  106.                   out float4 oDiffuse : COLOR0,
  107.                   out float4 oSpecular : COLOR1,
  108.                   out float2 oTex0 : TEXCOORD0 )
  109. {
  110.     // Transform the position from object space to homogeneous projection space
  111.     oPos = mul( vPos, g_mWorldViewProjection );
  112.  
  113.     // Compute the view-space position
  114.     float4 ViewPos = mul( vPos, g_mWorldView );
  115.  
  116.     // Compute view-space normal
  117.     float3 ViewNormal = normalize( mul( vNormal, (float3x3)g_mWorldView ) );
  118.  
  119.     // Compute diffuse lighting
  120.     oDiffuse = dot( ViewNormal, normalize( g_vLight - ViewPos ) ) * Diffuse;
  121.  
  122.     // Compute specular lighting
  123.     // Compute half vector
  124.     float3 vHalf = normalize( normalize( -ViewPos ) + normalize( g_vLight - ViewPos ) );
  125.  
  126.     // Compute normal dot half for specular light
  127.     oSpecular = pow( saturate( dot( vHalf, ViewNormal ) ) * Specular, Power );
  128.  
  129.     // Just copy the texture coordinate through
  130.     oTex0 = vTex0;
  131. }
  132.  
  133.  
  134. float4 PixScene1x( float4 MatDiffuse : COLOR0,
  135.                    float4 MatSpecular : COLOR1,
  136.                    float2 Tex0 : TEXCOORD0,
  137.                    float3 EnvTex : TEXCOORD1 ) : COLOR0
  138. {
  139.     // Lookup mesh texture and modulate it with diffuse
  140.     return ( MatDiffuse * tex2D( g_samScene, Tex0 ) + MatSpecular );
  141. }
  142.  
  143.  
  144. //-----------------------------------------------------------------------------
  145. // Name: RenderScene
  146. // Type: Technique
  147. // Desc: Renders scene to render target
  148. //-----------------------------------------------------------------------------
  149. technique RenderScene
  150. {
  151.     pass P0
  152.     {
  153.         VertexShader = compile vs_1_1 VertScene();
  154.         PixelShader  = compile ps_2_0 PixScene();
  155.         ZEnable = true;
  156.         AlphaBlendEnable = false;
  157.     }
  158. }
  159.  
  160.  
  161. technique RenderScene1x
  162. {
  163.     pass P0
  164.     {
  165.         VertexShader = compile vs_1_1 VertScene1x();
  166.         PixelShader  = compile ps_1_1 PixScene1x();
  167.         ZEnable = true;
  168.         AlphaBlendEnable = false;
  169.     }
  170. }
  171.